package com.example.JavaWebApi.service.impl.systemImpl;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.example.JavaWebApi.model.configmodel.OssConfig;
import com.example.JavaWebApi.service.systemService.IFileUploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

/**
 * @program: java-web-api
 * @description: 阿里云文件上传实现类
 * @author: 杨泽
 * @create: 2023-07-06 16:01
 */
@Service
@Slf4j
public class FileUploadServiceImpl implements IFileUploadService {
    @Autowired
    private OssConfig ossConfig;

    @Override
    public boolean uploadFile(String objectName, String filePath) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfig.getAliyunOssBucket(), objectName, new File(filePath));
        PutObjectResult result = upload(putObjectRequest);
        return true;
    }

    @Override
    public boolean uploadFile(String objectName, byte[] bytes) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfig.getAliyunOssBucket(), objectName, new ByteArrayInputStream(bytes));
        PutObjectResult result = upload(putObjectRequest);
        return true;
    }

    @Override
    public boolean uploadFile(String objectName, InputStream stream) {
        OSS client = getClient();
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfig.getAliyunOssBucket(), objectName, stream);
        PutObjectResult result = upload(putObjectRequest);
        return true;
    }

    private PutObjectResult upload(PutObjectRequest putObjectRequest) {
        OSS client = getClient();
        try {
            PutObjectResult result = client.putObject(putObjectRequest);
            return result;
        } catch (OSSException oe) {
            log.error("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            log.error("Error Message:" + oe.getErrorMessage());
            log.error("Error Code:" + oe.getErrorCode());
            log.error("Request ID:" + oe.getRequestId());
            log.error("Host ID:" + oe.getHostId());
            throw oe;
        } catch (ClientException ce) {
            log.error("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            log.error("Error Message:" + ce.getMessage());
            throw ce;
        } finally {
            if (client != null) {
                client.shutdown();
            }
        }
    }

    private OSS getClient() {
        return new OSSClientBuilder().build(ossConfig.getAliyunOssEndponit(), ossConfig.getAliyunOssKeyId(), ossConfig.getAliyunOssKeySecret());
    }
}